<template>
  <div class="login-container">
    <div class="login-box">
      <div class="tabs">
        <button :class="{ active: activeTab === 'login' }" @click="activeTab = 'login'">登 录</button>
        <button :class="{ active: activeTab === 'register' }" @click="$router.push('/Register')"">注 册</button>
      </div>
      
      <!-- 登录表单 -->
      <el-form v-show="activeTab === 'login'" ref="loginForm" :model="form" :rules="rules" label-width="0px">
        <el-form-item prop="account">
          <div class="input-group">
            <i class="el-icon-user input-icon"></i>
            <el-input v-model="form.account" placeholder="用户名" class="login-input" />
          </div>
        </el-form-item>
        
        <el-form-item prop="password">
          <div class="input-group">
            <i class="el-icon-lock input-icon"></i>
            <el-input v-model="form.password" type="password" placeholder="密码" class="login-input" />
          </div>
        </el-form-item>
        
        <el-form-item>
          <button type="button" @click="submitForm" class="login-button">
            登 录
          </button>
        </el-form-item>
      </el-form>
      
      <!-- 注册表单 -->
      <el-form v-show="activeTab === 'register'" ref="registerForm" :model="registerForm" :rules="registerRules" label-width="0px">
        <el-form-item prop="account">
          <div class="input-group">
            <i class="el-icon-user input-icon"></i>
            <el-input v-model="registerForm.account" placeholder="设置用户名" class="login-input" />
          </div>
        </el-form-item>
        
        <el-form-item prop="password">
          <div class="input-group">
            <i class="el-icon-lock input-icon"></i>
            <el-input v-model="registerForm.password" type="password" placeholder="设置密码" class="login-input" />
          </div>
        </el-form-item>
        
        <el-form-item prop="confirmPassword">
          <div class="input-group">
            <i class="el-icon-lock input-icon"></i>
            <el-input v-model="registerForm.confirmPassword" type="password" placeholder="确认密码" class="login-input" />
          </div>
        </el-form-item>
        
        <el-form-item>
          <button type="button" @click="submitRegisterForm" class="login-button">
            注 册
          </button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value !== this.registerForm.password) {
        callback(new Error('两次输入的密码不一致'));
      } else {
        callback();
      }
    };
    
    return {
      activeTab: 'login',
      form: {
        account: '123',
        password: '123456'
      },
      registerForm: {
        account: '',
        password: '',
        confirmPassword: ''
      },
      rules: {
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 20, message: '账号长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
        ]
      },
      registerRules: {
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 20, message: '账号长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' },
          { pattern: /^(?=.*[A-Za-z])(?=.*\d).+$/, message: '密码需包含字母和数字', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: validatePassword, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          // 原登录逻辑保持不变
          console.log('表单验证通过，开始登录请求');
          axios.post('http://localhost:9084/api/employee/login', {
            id: this.form.account,
            password: this.form.password
          })
            .then(response => {
              console.log('登录响应:', response);
              
              if (!response || !response.data) {
                console.error('响应数据为空');
                this.$message.error('登录失败：响应数据格式异常');
                return;
              }
              
              if (response.data.code === 200) {
                localStorage.setItem('token', response.data.token);
                this.$message.success('登录成功');
                this.$router.push({ name: 'Home' })
                  .catch(err => {
                    console.error('路由跳转错误:', err);
                    this.$message.error('路由跳转失败');
                  });
              } else {
                this.$message.error(response.data.message || '登录失败');
              }
            })
            .catch(error => {
              console.error('登录请求错误:', error);
              if (error.response) {
                this.$message.error(`登录失败：${error.response.status} ${error.response.statusText}`);
              } else {
                this.$message.error('登录失败：网络连接异常');
              }
            });
        } else {
          console.log('表单验证失败');
        }
      });
    },
    
    submitRegisterForm() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          // 注册逻辑
          console.log('注册请求开始');
          axios.post('http://localhost:9083/api/user/register', {
            username: this.registerForm.account,
            password: this.registerForm.password
          })
            .then(response => {
              console.log('注册响应:', response);
              
              if (response.data.code === 200) {
                this.$message.success('注册成功');
                // 注册成功后切换到登录标签页
                this.activeTab = 'login';
                // 自动填充新注册的用户名
                this.form.account = this.registerForm.account;
                // 清空注册表单
                this.$refs.registerForm.resetFields();
              } else {
                this.$message.error(response.data.message || '注册失败');
              }
            })
            .catch(error => {
              console.error('注册请求错误:', error);
              if (error.response) {
                this.$message.error(`注册失败：${error.response.data.message || '服务器错误'}`);
              } else {
                this.$message.error('注册失败：网络连接异常');
              }
            });
        } else {
          console.log('注册表单验证失败');
        }
      });
    },
    
    resetForm() {
      this.$refs.loginForm.resetFields();
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.login-box {
  background: white;
  padding: 40px;
  border-radius: 12px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
  width: 380px;
  text-align: center;
  color: #333;
  animation: fadeIn 0.6s ease-in-out;
}

.tabs {
  display: flex;
  margin-bottom: 30px;
  border-bottom: 1px solid #eee;
}

.tabs button {
  flex: 1;
  padding: 12px;
  background: none;
  border: none;
  font-size: 18px;
  font-weight: 600;
  color: #777;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  outline: none;
}

.tabs button.active {
  color: #409eff;
}

.tabs button.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 20%;
  width: 60%;
  height: 3px;
  background: #409eff;
  border-radius: 3px;
  transition: all 0.3s ease;
}

.input-group {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  transition: border-color 0.3s;
}

.input-group:focus-within {
  border-color: #409eff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

.input-icon {
  font-size: 18px;
  padding: 0 15px;
  color: #777;
}

.login-input {
  flex: 1;
}

.login-input >>> .el-input__inner {
  border: none !important;
  box-shadow: none !important;
  padding-left: 5px;
  height: 48px;
}

.login-button {
  width: 100%;
  height: 50px;
  padding: 0;
  border: none;
  background: linear-gradient(to right, #409eff, #2174ff);
  color: white;
  font-size: 18px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s;
  border-radius: 25px;
  letter-spacing: 8px;
  box-shadow: 0 4px 8px rgba(33, 116, 255, 0.3);
}

.login-button:hover {
  background: linear-gradient(to right, #2174ff, #005af0);
  transform: translateY(-2px);
  box-shadow: 0 6px 12px rgba(33, 116, 255, 0.4);
}

.login-button:active {
  transform: translateY(0);
}

/* 动画调整 */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}
</style>